package com.guaju.yueshangclothing.activity;

import android.Manifest;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.guaju.yueshangclothing.R;
import com.guaju.yueshangclothing.databinding.ActivityMainBinding;
import com.guaju.yueshangclothing.fragment.CartFragment;
import com.guaju.yueshangclothing.fragment.CategoryFragment;
import com.guaju.yueshangclothing.fragment.HomeFragment;
import com.guaju.yueshangclothing.fragment.MineFragment;
import com.guaju.yueshangclothing.viewmodel.MainVM;

import java.util.ArrayList;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;

public class MainActivity extends FragmentActivity {

    private static final int REQUEST_PERMISSION = 1000;
    private ActivityMainBinding binding;
    private ArrayList<Fragment> mFragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        //一体栏
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.home_background));
        //动态权限申请
        PermissionGen.with(MainActivity.this)
                .addRequestCode(REQUEST_PERMISSION)
                .permissions(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .request();
        initFragment();
        MainVM mainVM = new MainVM();
        binding.setViewmodel(mainVM);
    }

    private void initFragment() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new CategoryFragment());
        mFragmentList.add(new CartFragment());
        mFragmentList.add(new MineFragment());
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fl_container,mFragmentList.get(0));
        ft.commit();


    }


    @Override public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                                     int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionFail(requestCode = REQUEST_PERMISSION)
    public void doFailSomething(){
        Toast.makeText(this, "读取您的存储失败,为了您的用户体验，请您开启", Toast.LENGTH_SHORT).show();
    }

}
